package com.zoke.support.image;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.zoke.support.R;
import com.zoke.support.base.BaseObjectListAdapter;
import com.zoke.support.image.activity.GalleryActivity;
import com.zoke.support.image.activity.ShowImagesActivity;
import com.zoke.support.utils.LogUtil;
import com.zoke.support.utils.ViewHolder;
import com.zoke.support.view.ZokeLinearLayout;
import com.zoke.support.view.ZokeTextView;

/**
 * 分组相册（新布局）
 * 
 * @author JackWu
 * @version
 * @date 2014-11-20
 * @_QQ_ 651319154
 */
public class ImageGroupsGridAdapter extends BaseObjectListAdapter {

	private ZokeLinearLayout mContainerLl;
	private ImageView mImageIv;
	private ZokeTextView mNameTv;
	private ZokeTextView mCountTv;
	private int mMax;

	private HashMap<String, List<String>> mGruopMap;
	private int mType;

	public ImageGroupsGridAdapter(Context context, List<? extends Object> datas) {
		super(context, datas);
	}

	public ImageGroupsGridAdapter(Context context,
			List<? extends Object> datas,
			HashMap<String, List<String>> mGruopMap, int type,int max) {
		super(context, datas);
		this.mGruopMap = mGruopMap;
		this.mType = type;
		this.mMax = max;
	}

	@Override
	public View getItemView(int position, View convertView, ViewGroup parent) {
		convertView = getConvertView(convertView,
				R.layout.grideview_item_images);
		final ImageBean imageBeen = (ImageBean) mDatas.get(position);
		mContainerLl = ViewHolder.get(convertView, R.id._item_container);
		mImageIv = ViewHolder.get(convertView, R.id._image);
		mNameTv = ViewHolder.get(convertView, R.id._name);
		mCountTv = ViewHolder.get(convertView, R.id._count);
		mNameTv.setText(imageBeen.getFolderName());
		mCountTv.setText("(" + Integer.toString(imageBeen.getImageCounts())
				+ ")");
		String uri = "file:///" + imageBeen.getTopImagePath();
		LogUtil.i("处理图片--" + uri);
		ImageLoader.getInstance().displayImage(uri, mImageIv,
				ImageOptions.getThumbOptions());

		mContainerLl.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				List<String> childList = mGruopMap.get(imageBeen
						.getFolderName());
				Intent intent = new Intent(mContext, ShowImagesActivity.class);
				intent.putExtra(
						ShowImagesActivity.PHOTOSFIRSNAME,
						imageBeen.getFolderName() + "("
								+ Integer.toString(imageBeen.getImageCounts())
								+ ")");
				intent.putStringArrayListExtra(
						ShowImagesActivity.PHOTOSDATASSTRINGS,
						(ArrayList<String>) childList);
				// 将外部的操作需求发出给相册列表
				intent.putExtra(GalleryActivity.HANDLETYPE, mType);
				intent.putExtra(ShowImagesActivity.MAXMORE, mMax);
				startUIForResult(intent, mType);
			}
		});

		return convertView;
	}

}
